<template>
  <a-card :bordered="false">
    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-upload-dragger
        name="files"
        :multiple="true"
        :action="uploadLocalAction"
        :headers="tokenHeader"
        :showUploadList="false"
        v-show="isShow"
        @change="handleChange">
        <p class="ant-upload-drag-icon">
          <a-icon type="inbox"/>
        </p>
        <p class="ant-upload-text">上传文件</p>
      </a-upload-dragger>

    </div>


    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :loading="loading"
        :dataSource="dataSource"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handlePreview(record)">预览</a>
          <!--          <a href="https://view.officeapps.live.com/op/view.aspx?src=http://a3.att.hudong.com/14/75/01300000164186121366756803686.jpg" target="_blank">点我预览</a> -->
          <a-divider type="vertical"/>
          <a v-show="isShow" @click="ossDelete(record.id)">删除</a>
        </span>

      </a-table>
    </div>
    <!-- table区域-end -->
  </a-card>
</template>

<script>
  import { HjListMixin } from '@/mixins/HjListMixin'
  import { httpAction, postAction } from '@/api/manage'

  export default {
    name: 'LocalFile',
    mixins: [HjListMixin],
    data() {
      return {
        description: '文件列表',
        fk: '',
        queryParam: { fk: 'fk' },
        isShow:false,
        uploading: false,


        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            customRender: function(t, r, index) {
              return parseInt(index) + 1
            }
          },
          {
            title: '文件名称',
            align: 'center',
            width: 150,
            dataIndex: 'realName'
          },
          {
            title: '操作',
            dataIndex: 'action',
            width: 70,
            align: 'center',
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          upload: '/oss/file/upload',
          uploadLocal: '/oss/file/uploadByLocal',
          list: '/oss/file/list',
          delete: '/oss/file/delete',
          minioUpload: '/sys/upload/uploadMinio'
        }
      }
    },
    computed: {

      uploadAction() {
        return window._CONFIG['domianURL'] + this.url.upload
      },
      minioUploadAction() {
        return window._CONFIG['domianURL'] + this.url.minioUpload
      }
    },

    methods: {
      uploadLocalAction() {
        return window._CONFIG['domianURL'] + this.url.uploadLocal + '?fk=' + this.fk
      },

      sd(id,isShow) {
        this.isShow=isShow
        //this.fk=id
        if (id) {
          this.queryParam.fk = id
        } else {
          this.queryParam.fk = 'fk'
        }
        this.loadData()
      },

      handleChange(info) {
        this.$emit('delCheckFun', val => {
          this.fk = val
        }) // 传函数给父组件
        if (!this.fk) {
          this.uploading = false
          this.$message.error(`请先保存申请！`)
          return false
        }

        if (info.file.status === 'done') {
          if (info.file.response.success) {
            this.confirmLoading = false
            this.queryParam.fk = this.fk
            console.log(this.fk)
            this.loadData()
          } else {
            this.$message.error(`${info.file.response.message}`)
          }
        } else if (info.file.status === 'error') {
          this.$message.error(`${info.file.response.message}`)
        }
      },


      ossDelete(id) {
        var that = this
        that.$confirm({
          title: '确认删除',
          content: '是否删除选中文件?',
          onOk: function() {
            that.handleDelete(id)
          }
        })
      },
      handlePreview(record) {
        if (record && record.url) {
          //let url = window._CONFIG['onlinePreviewDomainURL'] + '?url=' + encodeURIComponent(record.url)
          //var url =window._CONFIG['domianURL']+"/generic/web/viewer.html?file="+encodeURIComponent("http://localhost:8081/hj/oss/file/download");
          var url = window._CONFIG['domianURL'] + '/oss/file/download?id=' + record.id
          window.open(url)
        }
      }
    }
  }
</script>

<style scoped>
  @import '~@assets/less/common.less';
</style>
